package com.mfkj.update.base

import android.content.Intent
import android.content.pm.ActivityInfo
import android.os.Bundle
import android.view.LayoutInflater
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import java.lang.reflect.ParameterizedType


abstract class BaseActivity<VB : ViewBinding> : AppCompatActivity() {

    protected val TAG = this::class.java.simpleName
    lateinit var binding: VB


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //保持屏幕常亮
        window.setFlags(
            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
        )
        //固定竖屏
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        initParams()
        initBinding()
        initView()
        initData()
    }

    open fun initParams() {
    }

//    override fun getResources(): Resources? {
//        val res: Resources? = super.getResources()
//
//        res?.let {
//            res.configuration?.apply {
//                if (fontScale != 1.0f) {
//                    fontScale = 1.0f
//                    createConfigurationContext(this)
//                }
//            }
//            AutoSizeCompat.autoConvertDensityOfGlobal(res) //如果没有自定义需求用这个方法
//        }
//        return res
//    }


    //利用反射，调用指定ViewBinding中的inflate方法填充视图
    private fun initBinding() {
        val type = javaClass.genericSuperclass
        if (type is ParameterizedType) {
            val clazz = type.actualTypeArguments[0] as Class<VB>
            val method = clazz.getMethod("inflate", LayoutInflater::class.java)
            binding = method.invoke(null, layoutInflater) as VB
            setContentView(binding.root)
        }

    }

    abstract fun initView()


    abstract fun initData()

    /**
     * 直接跳转activity
     */
    fun startActivity(clazz: Class<*>) {
        startActivity(Intent(this, clazz))
    }

}